package com.cat.chapter051;

import java.io.*;
import java.util.Arrays;
import java.util.Map;

/**
 * @author cat
 * @description https://www.nowcoder.com/practice/7037a3d57bbd4336856b8e16a9cafd71
 * @create 2025/7/22 10:27
 * @since JDK17
 */

public class Solution03 {
    static int MAXN = 100010;
    static int[] arr = new int[MAXN];
    static int n, max;
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out));
    static StreamTokenizer tokenizer = new StreamTokenizer(br);

    public static boolean check(long cur) {
        for (int i = 0; i < n; i++) {
            if (arr[i] > cur) {
                cur -= arr[i] - cur;
            } else {
                cur += cur - arr[i];
            }
            if (cur >= max) return true;
            if (cur < 0) return false;
        }
        return true;
    }
    public static void main(String[] args) throws IOException {
        tokenizer.nextToken();
        n = ((int) tokenizer.nval);
        for (int i = 0; i < n; i++) {   // 读入数据
            tokenizer.nextToken();
            arr[i] = ((int) tokenizer.nval);
            max = Math.max(max, arr[i]);
        }
        long l = 0, r = ((int)(1e5 + 10)), mid;
        while (l + 1 < r) {
            mid = l + (r - l) / 2;
            if (check(mid)) {   // 可以完成
                r = mid;
            } else {    // 不可以
                l = mid;
            }
        }
        pr.println(r);
        Arrays.fill(arr, 0, n + 1, 0);
        pr.flush();
        pr.close();
        br.close();
    }
}
